#!/bin/bash

# Apifox MCP服务器启动脚本

echo "========================================"
echo "      Apifox MCP Server 启动脚本"
echo "========================================"

# 显示系统信息
echo "系统架构: $(uname -m)"
echo "Python版本: $(python3 --version)"

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "❌ 错误: 未找到Python 3。请先安装Python 3。"
    exit 1
fi

# 检查是否存在虚拟环境
if [ ! -d "venv" ]; then
    echo "📦 创建虚拟环境..."
    python3 -m venv venv
    if [ $? -ne 0 ]; then
        echo "❌ 错误: 虚拟环境创建失败"
        exit 1
    fi
    echo "✅ 虚拟环境创建成功"
fi

# 激活虚拟环境
echo "🔄 激活虚拟环境..."
source venv/bin/activate
if [ $? -ne 0 ]; then
    echo "❌ 错误: 虚拟环境激活失败"
    exit 1
fi

# 验证虚拟环境
if [[ "$VIRTUAL_ENV" != "" ]]; then
    echo "✅ 虚拟环境已激活: $VIRTUAL_ENV"
else
    echo "❌ 警告: 虚拟环境可能未正确激活"
fi

# 安装依赖
echo "📥 安装依赖包..."
pip install -r requirements.txt --no-cache-dir
if [ $? -ne 0 ]; then
    echo "❌ 错误: 依赖包安装失败"
    exit 1
fi

# 启动服务器
echo "========================================"
echo "🚀 启动MCP服务器..."
echo "✅ 服务器已启动，等待连接..."
echo "💡 按 Ctrl+C 停止服务器"
echo "========================================"

python3 main.py 